/**
 * 
 */
package com.idea.auction.service;
import java.util.Date;

import com.idea.auction.model.RolePer;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.jfinal.server.BaseService;

/**
 * @author y.z
 */
public class RolePerService extends BaseService<RolePer> {
	

	
	
	/**
	 * 根据角色和权限获取
	 * @param roleId
	 * @param perId
	 * @return
	 */
	public RolePer findByRoleIdAndPerId(String roleId,int perId){
		return super.findFirst("select * from sys_role_per where role_id = "+roleId+" and per_id = '"+perId+"'");
	}
	
	@Before(Tx.class)
	public boolean addRolePer(String roleId,String perId){
		
		super.update("delete from sys_role_per where role_id = "+roleId+"");
		String perIds [] = perId.split(",");
		
		for(String pId:perIds){
			RolePer rolePer = new RolePer()
			.set("role_id", roleId)
			.set("per_id", pId)
			.set("create_date", new Date());
			super.save(rolePer);
		}
		
		return true;
	}
}
